package com.jingan.nursinghome.regulatory.dialog.businessside;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextView;

import com.jingan.nursinghome.regulatory.R;
import com.jingan.nursinghome.regulatory.data.MicrometerTable;
import com.jingan.nursinghome.regulatory.http.Http;
import com.jingan.nursinghome.regulatory.model.Images;
import com.jingan.nursinghome.regulatory.model.Qfinfo;
import com.jingan.nursinghome.regulatory.model.QfinfoTopic;
import com.jingan.nursinghome.regulatory.view.MyActionBar;
import com.jingan.nursinghome.regulatory.view.TriangelView2;

import java.util.ArrayList;
import java.util.List;

import cn.android.support.v7.lib.sin.crown.base.BaseActivity;
import cn.android.support.v7.lib.sin.crown.base.BaseDialog;
import cn.android.support.v7.lib.sin.crown.base.BaseListenner;
import cn.android.support.v7.lib.sin.crown.builder.BitmapBuilder;
import cn.android.support.v7.lib.sin.crown.utils.ProportionUtils;

/**
 * 评分结果页
 * Created by 彭治铭 on 2018/4/1.
 */

public class MicrometerTableResultDialog extends BaseDialog implements View.OnClickListener {
    private static MicrometerTableResultDialog resultDialog;

    static String iid;//巡检信息id

    public static MicrometerTableResultDialog getInstance(BaseActivity baseActivity, String id) {
        iid = id;
        if (isfinish(baseActivity, resultDialog)) {
            resultDialog = new MicrometerTableResultDialog(baseActivity);
        }
        return resultDialog;
    }

    public MicrometerTableResultDialog(BaseActivity baseActivity) {
        init(baseActivity, R.layout.dialog_business_results);
    }

    MyActionBar myActionBar;
    Button btn_back, btn_next;

    TextView txt_signature;
    TextView txt_img1, txt_img2, txt_img3, txt_img4, txt_img5;

    TextView txt_hdfs2,txt_hdfs4,txt_name,txt_name2,txt_jigou,txt_time,txt_goutou,txt_beizhu2;
    @Override
    protected void initUI() {
        myActionBar = (MyActionBar) findViewById(R.id.MyActionBar);
        myActionBar.setLeftBimapAndTitle(null, "评分结果");
        myActionBar.setBaseListennerLeft(new BaseListenner() {
            @Override
            public void onResult(Object result) {
                MicrometerTableDetailsDialog.getInstance(getBaseActivity(), iid);
                dismiss();
            }
        });
        btn_back = (Button) findViewById(R.id.btn_back);
        btn_back.setOnClickListener(this);
        btn_next = (Button) findViewById(R.id.btn_next);
        btn_next.setOnClickListener(this);
        txt_time = (TextView) findViewById(R.id.txt_time);
        txt_signature = (TextView) findViewById(R.id.txt_signature);

        txt_img1 = (TextView) findViewById(R.id.txt_img1);
        txt_img2 = (TextView) findViewById(R.id.txt_img2);
        txt_img3 = (TextView) findViewById(R.id.txt_img3);
        txt_img4 = (TextView) findViewById(R.id.txt_img4);
        txt_img5 = (TextView) findViewById(R.id.txt_img5);

        txt_hdfs2= (TextView) findViewById(R.id.txt_hdfs2);
        txt_hdfs4= (TextView) findViewById(R.id.txt_hdfs4);
        txt_name= (TextView) findViewById(R.id.txt_name);
        txt_name2= (TextView) findViewById(R.id.txt_name2);
        txt_jigou= (TextView) findViewById(R.id.txt_jigou);
        txt_goutou= (TextView) findViewById(R.id.txt_goutou);
        txt_beizhu2= (TextView) findViewById(R.id.txt_beizhu2);
    }


    Qfinfo qfinfo;

    @Override
    protected void listener() {
        qfinfo = MicrometerTable.getInstance().getQfinfo();
        txt_hdfs2.setText(qfinfo.getTotal_hair() + "分"+"/"+qfinfo.getTotal() + "分");//得分/总分
        txt_hdfs4.setText(qfinfo.getScoring_rate() + "%");//得分率
        txt_name.setText("套题名称：");
        txt_jigou.setText("巡检机构："+qfinfo.getJly());//养老机构
        txt_time.setText("巡检时间："+qfinfo.getDate());//巡检日期
        txt_goutou.setText("主要沟通人员："+qfinfo.getLname());//主要沟通人员
        txt_beizhu2.setEnabled(false);


        txt_beizhu2.setText("这个人很懒，什么都没有留下。。。");//备注
        txt_signature.setText("暂无签名图片");
        Http.getInstance().qfinfoCase(getActivity(), iid, new BaseListenner<List<Qfinfo>>() {

            @Override
            public void onResult(List<Qfinfo> result) {
                qfinfo.setSignature(result.get(0).getSignature());//签名
                //Log.e("test", "签名:\t" + qfinfo.getSignature());
                qfinfo.setLname(result.get(0).getLname());//主要沟通人员
                qfinfo.setName(result.get(0).getName());//套题名称。
                if (result.get(0).getRemark() != null && result.get(0).getRemark().trim().length() > 0) {
                    qfinfo.setRemark(result.get(0).getRemark());//备注
                    txt_beizhu2.setText(qfinfo.getRemark());
                }
                txt_goutou.setText("主要沟通人员："+qfinfo.getLname());//主要沟通人员
                txt_name2.setText(qfinfo.getName());//套题名称

                BitmapBuilder.Builder(Http.getInstance().qianming + qfinfo.getSignature()).execute(getActivity(), null, new BaseListenner<Bitmap>() {
                    @Override
                    public void onResult(Bitmap result) {
                        float sp = (float) result.getHeight() / (float) result.getWidth();
                        Bitmap bitmap = ProportionUtils.getInstance().GeometricCompressionBitmap(result, txt_signature.getWidth(), sp);
                        txt_signature.getLayoutParams().width = bitmap.getWidth();
                        txt_signature.getLayoutParams().height = bitmap.getHeight();
                        txt_signature.requestLayout();
                        txt_signature.setBackground(new BitmapDrawable(bitmap));
                        txt_signature.setText("");
                    }
                });
            }
        });

        lilist.clear();
        lilist.add(findViewById(R.id.li1));
        lilist.add(findViewById(R.id.li2));
        lilist.add(findViewById(R.id.li3));
        lilist.add(findViewById(R.id.li4));
        lilist.add(findViewById(R.id.li5));
        for(int i=0;i<lilist.size();i++){
            lilist.get(i).setVisibility(View.GONE);
        }

        txtList.clear();
        txtList.add(txt_img1);
        txtList.add(txt_img2);
        txtList.add(txt_img3);
        txtList.add(txt_img4);
        txtList.add(txt_img5);
        Http.getInstance().qfinfoImg(getActivity(), iid, new BaseListenner<List<Images>>() {
            @Override
            public void onResult(List<Images> result) {
                for (int i = 0; i < result.size(); i++) {
                    lilist.get(i).setVisibility(View.VISIBLE);
                    upImg(txtList.get(i),Http.getInstance().img +result.get(i).getPolling_img());

                }
            }
        });
    }
    private List<TextView> txtList=new ArrayList<>();
    private List<View> lilist=new ArrayList<>();
    private void upImg(final TextView txt_signature,String url){
        BitmapBuilder.Builder(url).execute(getActivity(), null, new BaseListenner<Bitmap>() {
            @Override
            public void onResult(Bitmap result) {
                float sp = (float) result.getHeight() / (float) result.getWidth();
                Bitmap bitmap = ProportionUtils.getInstance().GeometricCompressionBitmap(result, txt_signature.getWidth(), sp);
                txt_signature.getLayoutParams().width = bitmap.getWidth();
                txt_signature.getLayoutParams().height = bitmap.getHeight();
                txt_signature.requestLayout();
                txt_signature.setBackground(new BitmapDrawable(bitmap));
                txt_signature.setText("");
            }
        });
    }

    @Override
    protected void recycleView() {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_back://上一项
                MicrometerTableDetailsDialog.getInstance(getBaseActivity(), iid);
                dismiss();
                break;
            case R.id.btn_next://主页
                dismiss();
                break;
        }
    }
}
